package ar.com.quickmart.classes;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import ar.com.quickmart.R;
/*
 * Esta clase sirve para visualizar correctamente una lista con los datos
 * de los productos. Se define que datos va a contener cada fila de la lista,
 * y hace referencia al layout "R.layout.list_product", que contiene los elementos
 * visuales para cada fila de la lista de productos.
 */
public class PurchasableAdapter extends BaseAdapter {

	private final Context mContext;
	private final List<Purchasable> mPurchasables;
	
	public PurchasableAdapter(Context context, List<Purchasable> purchasables) {
		super();
		mContext = context;
		mPurchasables = purchasables;
	}
	
	@Override
	public int getCount() {
		return mPurchasables.size();
	}

	@Override
	public Object getItem(int position) {
		return mPurchasables.get(position);
	}

	@Override
	public long getItemId(int position) {
		return mPurchasables.get(position).getId();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		// La vista que contiene el layout de la fila voy a devolver
		View row = convertView;
		
		// FIXME Verifica clase, esto deberia ser transparente
		if(mPurchasables.get(position).getClass() == Product.class) {
			
			ProductHolder holder = new ProductHolder();
			
			//if(row == null) {
				
				LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				row = inflater.inflate(R.layout.list_item_product, parent, false);
				
				holder.txtProductName = (TextView) row.findViewById(R.id.productName);
				holder.txtPrice = (TextView) row.findViewById(R.id.productPrice);
				holder.txtBarcode = (TextView) row.findViewById(R.id.productBarCode);
				holder.txtNumber = (TextView) row.findViewById(R.id.productNumber);
				
				row.setTag(holder);			
	/*		}
			else {
						holder = (ProductHolder) row.getTag();
			}		*/
	
			holder.txtProductName.setText(mPurchasables.get(position).getName());
			holder.txtProductName.setTextColor(Color.BLACK);
			holder.txtBarcode.setText(String.valueOf(mPurchasables.get(position).getId()) + " ");
			if(mPurchasables.get(position).getPrice() > 0) {
				holder.txtPrice.setText("$" + String.format("%.2f", (mPurchasables.get(position).getPrice() * mPurchasables.get(position).getCount())) + " " );
			} else {
				holder.txtPrice.setText("$ -");
			}
			holder.txtNumber.setText(String.valueOf("$" + String.format("%.2f", mPurchasables.get(position).getPrice()) + " x " + mPurchasables.get(position).getCount() + "u."));
		}
		else if(mPurchasables.get(position).getClass() == Promo.class) {
			
			PromoHolder holder = new PromoHolder();
			
//				if(row == null) {
				
				LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				row = inflater.inflate(R.layout.list_item_promo, parent, false);
				
				holder.txtPromoName = (TextView) row.findViewById(R.id.productName);
				holder.txtPrice = (TextView) row.findViewById(R.id.productPrice);
				holder.txtId = (TextView) row.findViewById(R.id.productBarCode);
				holder.txtCount = (TextView) row.findViewById(R.id.productNumber);
				
				row.setTag(holder);			
/* 			}
			else {
				holder = (PromoHolder) row.getTag();
			}  */		
	
			holder.txtPromoName.setText(mPurchasables.get(position).getName());
			holder.txtId.setText(String.valueOf(mPurchasables.get(position).getId()) + " ");
			holder.txtPrice.setText("$" + String.format("%.2f", (mPurchasables.get(position).getPrice() * mPurchasables.get(position).getCount())) + " " );
			holder.txtCount.setText(String.valueOf("$" + String.format("%.2f", mPurchasables.get(position).getPrice()) + " x " + mPurchasables.get(position).getCount() + "u."));
		}
	
		return row;
	}
	
	private static class ProductHolder {
		public TextView txtProductName;
		public TextView txtPrice;
		public TextView txtBarcode;
		public TextView txtNumber;
	}
	
	private static class PromoHolder {
		public TextView txtPromoName;
		public TextView txtPrice;
		public TextView txtId;
		public TextView txtCount; 
	}

}
